%%%----------------------------------------------------------------------
%%% File    : cus_ejabberd_c2s.erl
%%% Author  : chen.kangmin
%%%----------------------------------------------------------------------

-module(cus_util).
-author('chen.kangmin').

-include("cus.hrl").


-export([
    to_list/1,
    str_to_number/1,
    phash/2,
    get_session_id/2,
    login/2,
    filter/1,
    format_iso8601/1,
    getSeq/1,
    get_emotion_packets/2,
    uuid/0,
    utc_to_datetime/1,
    utc_str/0
        ]).



to_list(Prop) ->
    case Prop of
        undefined ->
            "";
        B when is_binary(B) ->
            binary_to_list(B);
        I when is_integer(I) ->
            integer_to_list(I);
        F when is_float(F) ->
            lists:flatten(io_lib:format("~.3f", [F]));
        A when is_atom(A) ->
            atom_to_list(A);
        L when is_list(L) ->
            L;
        _ ->
            Prop
    end.


str_to_number(Str) ->
    try
        case string:chr(Str, $.) > 0 of
            true  -> list_to_float(Str);
            false -> list_to_integer(Str)
        end
    catch
        _Type:_Any -> undefined
    end.


phash(Key, Size) when is_list(Key) ->
    phash(?l2b(Key), Size);
phash(Key, Size) ->
    Size1 = size(Key) * 8,
    <<Num : Size1>> = Key,
    if
        Size > 1 ->
            (Num rem Size) + 1;
        true ->
            1
    end.


get_session_id(UserName,PassWord) ->
    UserName ++"_" ++ PassWord.

login(UserName,PassWord) ->
    if
        "111" =:= UserName , "111" =:=PassWord ->
            true;
        true ->
            false
    end.

filter(CookieVal) ->
    case CookieVal of
        undefined ->
            undefined;
        _->
            [UserName, PassWord] = string:tokens(?b2l(CookieVal),"_"),
            {login(UserName,PassWord),UserName,PassWord}
    end.




format_iso8601({{Y,Mo,D}, {H,Mn,S}}) when is_float(S) ->
    FmtStr = "~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~9.6.0f",
    IsoStr = io_lib:format(FmtStr, [Y, Mo, D, H, Mn, S]),
    list_to_binary(IsoStr);

format_iso8601({{Y,Mo,D}, {H,Mn,S}}) ->
    FmtStr = "~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0B",
    IsoStr = io_lib:format(FmtStr, [Y, Mo, D, H, Mn, S]),
    list_to_binary(IsoStr).






getSeq(Str) ->
    L = string:tokens((Str),"_"),
    ?l2i(lists:last(L)).


get_emotion_packets([], Acc) ->
    Acc;
get_emotion_packets([Id | T], Acc) ->
    case cus_redis:get_sys_emotion_packet(?i2l(Id)) of
        undefined ->
            get_emotion_packets(T, Acc);
        PacketInfo ->
            get_emotion_packets(T, [PacketInfo | Acc])
    end.

uuid() ->
    <<A:32, B:16, C:16, D:16, E:48>> = crypto:rand_bytes(16),
    Str = io_lib:format("~8.16.0b-~4.16.0b-4~3.16.0b-~4.16.0b-~12.16.0b",
        [A, B, C band 16#0fff, D band 16#3fff bor 16#8000, E]),
    binary_to_list(list_to_binary(Str)).

utc_str() ->
    {MegaSecs, Secs, _MicroSecs} = os:timestamp(),
    to_list(MegaSecs * 1000000 + Secs).

utc_to_datetime(Seconds) ->
    calendar:now_to_universal_time({Seconds div 1000000, Seconds rem 1000000, 0}).
